-- 植物卡类
local Object = require("app.mode.Object")

local PlantCard = {}
PlantCard.__index = PlantCard
setmetatable(PlantCard, Object)

-- 植物卡状态：冷却中=0 阳光不足=1 完成=2
PlantCard.COOLING = 0
PlantCard.LACK_SUN = 1
PlantCard.FINISH = 2

local sfc = cc.SpriteFrameCache:getInstance()

function PlantCard:initView()
    self._sprite = cc.Sprite:createWithSpriteFrame(sfc:getSpriteFrameByName(self:getName()))
end

-- 开始循环
function PlantCard:startLoop()
    -- 初始状态
    if self._scene:getTotalSun() >= self:getNeedSunShine() then
        self:setState(PlantCard.FINISH)
        self:getSprite():setSpriteFrame(sfc:getSpriteFrameByName(self:getName()))
    else
        self:setState(PlantCard.LACK_SUN)
        self:getSprite():setSpriteFrame(sfc:getSpriteFrameByName(self:getName() .. "_disable"))
    end

    self._timer =
        cc.Director:getInstance():getScheduler():scheduleScriptFunc(
        function(delta)
            if isPause then
                return
            end

            if self:getState() == PlantCard.COOLING then
                self:setTime(self:getTime() + 0.1)
                -- 冷却中：判断冷却是否完成
                if self:getTime() >= self:getNewWaitTime() then
                    self:setTime(0)
                    if self._scene:getTotalSun() >= self:getNeedSunShine() then
                        -- 转为完成状态
                        self:setState(PlantCard.FINISH)
                        self:getSprite():setSpriteFrame(sfc:getSpriteFrameByName(self:getName()))
                    else
                        -- 转为阳光不足状态
                        self:setState(PlantCard.LACK_SUN)
                    end
                end
            elseif self:getState() == PlantCard.LACK_SUN then
                -- 冷却完成但阳光不足：判断阳光是否充足了
                if self._scene:getTotalSun() >= self:getNeedSunShine() then
                    -- 转为完成状态
                    self:setState(PlantCard.FINISH)
                    self:getSprite():setSpriteFrame(sfc:getSpriteFrameByName(self:getName()))
                end
            elseif self:getState() == PlantCard.FINISH then
                -- 完成状态：判断阳光是否不足了
                if self._scene:getTotalSun() < self:getNeedSunShine() then
                    -- 转为阳光不足状态
                    self:setState(PlantCard.LACK_SUN)
                    self:getSprite():setSpriteFrame(sfc:getSpriteFrameByName(self:getName() .. "_disable"))
                end
            end
        end,
        0.1,
        false
    )
end

-- 停止循环
function PlantCard:stopLoop()
    if self._timer then
        cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self._timer)
        self._timer = nil
    end
end

-- 添加到显示列表
function PlantCard:addToDisplay(parent, x, y)
    if self._sprite and parent then
        parent:addChild(self._sprite)
    end
    if x then
        self:setPosition(x, y)
    end
end

-- 冷却
function PlantCard:cool()
    self:setState(PlantCard.COOLING)
    self:getSprite():setSpriteFrame(sfc:getSpriteFrameByName(self:getName() .. "_disable"))
end

-- 界面显示相关
function PlantCard:getTag()
    return self._tag
end

function PlantCard:setTag(val)
    self._tag = val
end

-- 数据
function PlantCard:getState()
    return self._data.state
end

function PlantCard:setState(val)
    self._data.state = val
end

function PlantCard:getNeedSunShine()
    return self._data.need_sun_shine
end

function PlantCard:getNewWaitTime()
    return self._data.new_wait_time
end

function PlantCard:getTime()
    return self._data.time
end

function PlantCard:setTime(val)
    self._data.time = val
end

return PlantCard
